/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "test.h"

int test_fs() {
    int fd = Sopen("file.txt", DO_WRONLY | DO_CREAT | DO_TRUNC, 0644);
    const char *data = "Hello,StadionOS!";
    Swrite(fd,data,Sstrlen(data));
    Sclose(fd);

    const char *dir_name = "test_dir";

    if (Smkdir(dir_name, 0755) == -1) {
        Sperror("Failed to create directory");
        return EEXIST;
    }

    TDIR *dir = Sopendir(dir_name);
    if (!dir) {
        Sperror("Failed to open directory");
        return ENOTDIR;
    }

    struct Tlinux_dirent *entry;
    while ((entry = Sreaddir(dir)) != NULL) {
        Sprint("Found entry: %s\n", entry->d_name);
    }

    if (Sclosedir(dir) == -1) {
        Sperror("Failed to close directory");
        return EIO;
    }

    if (Srmdir(dir_name) == -1) {
      Sperror("Failed to remove directory");
      return ENOTEMPTY;
    }

    Sprint("Directory operations completed successfully.\n");

    struct stat file_stat;

    const char *filename = "file.txt";
    if (Sstat(filename, &file_stat) == 0) {
        Sprint("File: %s\n", filename);
        Sprint("Inode number: %lu\n", file_stat.st_ino);
        Sprint("File size: %lld bytes\n", (long long)file_stat.st_size);
        Sprint("File type: ");
        if (S_ISREG(file_stat.st_mode)) {
            Sprint("Regular file\n");
        } else if (S_ISDIR(file_stat.st_mode)) {
            Sprint("Directory\n");
        } else {
            Sprint("Other\n");
        }
    } else {
        Sprint("Sstat failed: %s\n", Sstrerror(-Sstat(filename, &file_stat)));
    }

    int dfd = open(filename, DO_RDONLY);
    if (dfd == -1) {
        Sperror("open");
        exit(EXIT_FAILURE);
    }

    if (Sfstat(dfd, &file_stat) == 0) {
        Sprint("fstat on file descriptor %d:\n", dfd);
        Sprint("Inode number: %lu\n", file_stat.st_ino);
        Sprint("File size: %lld bytes\n", (long long)file_stat.st_size);
        Sprint("File type: ");
        if (S_ISREG(file_stat.st_mode)) {
            Sprint("Regular file\n");
        } else if (S_ISDIR(file_stat.st_mode)) {
            Sprint("Directory\n");
        } else {
            Sprint("Other\n");
        }
    } else {
        Sprint("Sfstat failed: %s\n", Sstrerror(-Sfstat(dfd, &file_stat)));
    }

    close(dfd);

    const char* path = "/home/user/example.txt";
    const char* file = Sbasename(path);
    Sprint("The file name is: %s\n", filename);

    char* directory = Sdirname(path);
    Sprint("The directory is: %s\n", directory);
    Sfree(directory);

    return 0;
}
